package com.okhttpdemo.ui;

import android.os.Bundle;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;

import com.okhttpdemo.R;
import com.okhttpdemo.api.Api;
import com.okhttpdemo.bean.MyResponse;
import com.okhttpdemo.bean.User;
import com.okhttpdemo.http.BaseCallBack;
import com.okhttpdemo.http.DownloadCallback;
import com.okhttpdemo.http.HttpUtils;
import com.okhttpdemo.http.UploadRequestBody;
import com.okhttpdemo.utils.StringUtils;

import java.io.File;
import java.io.IOException;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import okhttp3.Call;
import okhttp3.MultipartBody;
import okhttp3.Request;
import okhttp3.RequestBody;


public class MainActivity extends AppCompatActivity {

    @BindView(R.id.tv)
    TextView tv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
    }

    public void click(View v) {
        switch (v.getId()) {
            case R.id.getString:
                HttpUtils.newCall(new Request.Builder().url(String.format(Api.getString, "huang", "1234556")).get().build())
                        .enqueue(this, true, new BaseCallBack<String>() {
                            @Override
                            public void onResponse(Call call, MyResponse<String> response) throws IOException {
                                super.onResponse(call, response);
                                tv.setText(response.getBody());
                            }
                        });
                break;
            case R.id.getUser:
                HttpUtils.newCall(new Request.Builder().url(Api.getUser).get().build())
                        .enqueue(this, true, new BaseCallBack<User>() {
                            @Override
                            public void onResponse(Call call, MyResponse<User> response) throws IOException {
                                super.onResponse(call, response);
                                tv.setText(response.getBody().toString());
                            }
                        });
                break;
            case R.id.getUserList:
                HttpUtils.newCall(new Request.Builder().url(Api.getUserList).get().build())
                        .enqueue(this, true, new BaseCallBack<List<User>>() {
                            @Override
                            public void onResponse(Call call, MyResponse<List<User>> response) throws IOException {
                                super.onResponse(call, response);
                                tv.setText(response.getBody().toString());
                            }
                        });
                break;
            case R.id.downloadfile:
                HttpUtils.newCall(new Request.Builder().url(Api.downloadFile).get().build())
                        .enqueue(this, true, new DownloadCallback(Environment.getExternalStorageDirectory().getAbsolutePath(), "aaa.apk", HttpUtils.getUiExecutor()) {
                            @Override
                            public void onProgress(long sended, long totalLength) {
                                super.onProgress(sended, totalLength);
                                tv.setText("进度=" + StringUtils.floatScale(1.0 * sended / totalLength, 2));
                            }
                        });
                break;
            case R.id.uploadFile:
                //upload
                BaseCallBack<String> callBack = new BaseCallBack<String>() {
                    @Override
                    public void onProgress(long sended, long totalLength) {
                        super.onProgress(sended, totalLength);
                        tv.setText("进度=" + StringUtils.floatScale(1.0 * sended / totalLength, 2));
                    }
                };
                File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath(), "投融资平台.apk");
                MultipartBody.Builder builder = new MultipartBody.Builder().setType(MultipartBody.FORM).addFormDataPart("file", file.getName(), RequestBody.create(MultipartBody.FORM, file));
                HttpUtils.newCall(new Request.Builder().url(Api.uploadfile).post(new UploadRequestBody(HttpUtils.getUiExecutor(), builder.build(), callBack)).build())
                        .enqueue(this, callBack);
                break;
        }
        if (false) {
            //download
            HttpUtils.newCall(new Request.Builder().url("www.baidu.com").build())
                    .enqueue(this, new BaseCallBack() {
                    });


        }
    }
}
